#!/bin/bash

echo "===================================================="
echo "  __  _ __  __ _  __  __  "
echo " |  \| |\ \/ /| ||  \/  | "
echo " |_|\__| \__/ |_||_|\/|_|   by @lmk"
echo "                          "

# 检查 Python 是否已安装
if ! command -v python3 &> /dev/null
then
    echo -e "[\e[31mERROR\e[0m] Python 3 is not installed. Please install Python 3 and try again."
    exit 1
fi

# 获取 Python 版本
python_version=$(python3 --version)
echo -e "[\e[32mINFO\e[0m] Python version: $python_version."

# 检查 pip 是否已安装
if ! command -v pip3 &> /dev/null
then
    echo -e "[\e[31mERROR\e[0m] pip3 is not installed. Please install pip and try again."
    exit 1
fi

# 检查 jedi 是否已安装
if ! pip3 show jedi &> /dev/null
then
    echo -e "[\e[33mWARNING\e[0m] The 'jedi' package is not installed."
    read -p "Do you want to install it? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]
    then
        pip3 install jedi -i https://pypi.tuna.tsinghua.edu.cn/simple
    else
        echo -e "[\e[32mINFO\e[0m] Please install 'jedi' manually to use this feature."
    fi
else
    echo -e "[\e[32mINFO\e[0m] The 'jedi' package is installed."
fi

# 检查 pynvim 是否已安装
if ! pip3 show pynvim &> /dev/null
then
    echo -e "[\e[33mWARNING\e[0m] The 'pynvim' package is not installed."
    read -p "Do you want to install it? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]
    then
        pip3 install pynvim -i https://pypi.tuna.tsinghua.edu.cn/simple
    else
        echo -e "[\e[32mINFO\e[0m] Please install 'pynvim' manually to use this feature."
    fi
else
    echo -e "[\e[32mINFO\e[0m] The 'pynvim' package is installed."
fi

echo -e "[\e[32mINFO\e[0m] All checks completed."


# 安装 neovim
mkdir -p "$HOME/.opt"
tar -C $HOME/.opt -xzf nvim-linux64.tar.gz
echo 'export PATH="$PATH:$HOME/.opt/nvim-linux64/bin"' >> ~/.bashrc
echo -e "[\e[32mINFO\e[0m] Neovim installed to ~/.opt/nvim-linux64."

# 复制 neovim deoplete jedi 配置到 ~/.config/nvim
mkdir -p "$HOME/.config"
cp -r ./nvim ~/.config
echo -e "[\e[32mINFO\e[0m] Neovim config copied to ~/.config/nvim."

echo "===================================================="

bash
